package model

import (
	"log"
)

/*
mysql 创建sql
CREATE TABLE `student` (
`stu_id` INT(11) NOT NULL AUTO_INCREMENT,
`stu_name` VARCHAR(64) NOT NULL,
`stu_sex` VARCHAR(256) NOT NULL ,
PRIMARY KEY (`stu_id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

*/

type Student struct {
	StuId   int64  `xorm:"pk notnull autoincr 'stu_id'"`
	StuName string `xorm:"notnull 'stu_name'"`
	StuSex  string `xorm:"notnull 'stu_sex'"`
}

/*
增加学生的方法
*/
func AddStu(stu Student) (row int64) {
	row, err := Engine.Insert(stu)
	if err != nil {
		log.Println("insert stu err = %v", err)
	}
	return row
}
func GetStu() (listStu []Student) {
	listStu = make([]Student, 0)
	err := Engine.Limit(100, 0).Find(&listStu)
	if err != nil {
		log.Println("get stu err = %v", err)
		return nil
	}
	return
}
func DeleteStuByStuId(stuId int64) (row int64) {
	stu := new(Student)
	var err error
	row, err = Engine.Where("stu_id= ?", stuId).Delete(stu)
	if err != nil {
		log.Println("deleter err : %v", err)
		return 0
	}
	log.Println("success delete row = %d", row)
	return
}

/*
以后可以尝试的
func testSession()  {
	//事务的创建
	session:=dbUtil_xorm.Engine.NewSession()
	//开始事务
	session.Begin()
	session.Update()
	//回归操作
	session.Rollback()
	// 事务的提交
	session.Commit()
}

*/
